<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="com.stone.para.IConstant"%>
<%
	String contextPath = request.getContextPath();
%>
<html>  
<head>  
    <title>ExtSystem</title>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title><%=IConstant.PROGRAEM_TITLE%></title>
    <link rel="stylesheet" href="<%=contextPath%>/ext3/resources/css/ext-all.css" type="text/css"   />
    <script src="<%=contextPath%>/ext3/adapter/ext/ext-base.js"></script>
    <script src="<%=contextPath%>/ext3/ext-all.js"></script>
    <script type="text/javascript"> 
    	
        Ext.BLANK_IMAGE_URL = '<%=contextPath%>/ext3/resources/images/default/s.gif';
        Ext.QuickTips.init();
        Ext.form.Field.prototype.msgTarget = 'qtip';
    	var cpath = "<%=contextPath%>";

 		var contentTabs;
 		var formBase;
 		var layout;
 		var gridUser;
 		var storeGrid;
 		var treeRole;
 		
 		var ym = '<%=session.getAttribute("KQRQ")%>';

        Ext.onReady(function(){
        
        	tab1Build();
        	
  			Date.prototype.toString=function(){
 				 return this.format("Y-m");
 			};
 			
        });
 
        var tab1Build = function() {
         
			// 班组列表
			treeRole = new Ext.tree.TreePanel({
            	 id:'treeRole',
                 root : new Ext.tree.TreeNode({   
                    			id : "root", 
                    			draggable:false, 
                    			singleClickExpand:true, 
                    			text : "<%=IConstant.PROGRAEM_TITLE%>"
	 			}),  
                border:false,
                store : storeSQ, 
                rootVisible:false,
        		useArrows:true,
        		autoScroll:true,
		        containerScroll:true,
		        listeners:{"click":function(node){ 
		        
		        	/**if (node.attributes.qtip + "" != "000") {
		        		Ext.MessageBox.alert('提示', "请选择人员");
		        		return;
		        	}*/
		        	if ((node.attributes.qtip + "" ).length != 9) {
						Ext.MessageBox.alert('提示','请选择班组');
						return;
					}
		        	formBase.getForm().findField("entity.id").setValue("0");
					formBase.getForm().findField("username").setValue(node.text);
					formBase.getForm().findField("entity.bzid").setValue(node.id);
					formBase.getForm().findField("entity.strKhrq").setValue(ym);
					storeGrid.load();
                }}
            });
            
    		var storeSQ = new Ext.data.Store({
        		proxy:new Ext.data.HttpProxy({url:'userList!getListExtTree.do?state=bz'}),
        		reader:new Ext.data.JsonReader({},['id','text','qtip','parentId']),
        		autoLoad:true,
        		listeners:{"load":function(store,records){
 
        			if (records != null && records != "" && records.length > 0) {
        							
        				var baseLength = (records[0].data.qtip + "").length;

						for (var i=0; i<records.length; i++){
						
	       					var n = new Ext.tree.TreeNode({
                								id:records[i].data.id,
                								text:records[i].data.text,
                								qtip:records[i].data.qtip,
                								leaf:true
							});
											
							// 第一层菜单时
							if (i == 0) {
								treeRole.root.appendChild(n);
								// 下级菜单
							} else {
							
								try {
									if (n.attributes.qtip =="000") {
										n.attributes.icon = cpath + '/images/user_suit.png';
									}
    								treeRole.getNodeById(records[i].data.parentId).leaf = false;
    								treeRole.getNodeById(records[i].data.parentId).appendChild(n);			
    							} catch (e) {
    							}
							}
   						}  // for (var i=0; i<records.length; i++){

					 treeRole.root.expandChildNodes();
				}   // if (records != null && records != "" && records.length > 0) {
            }}
                    			 
    	});
			
		// 基本信息工具栏
		var toolbar1 = new Ext.Toolbar({
 				items: [{	id :'addDicButton',
							text :'增加',
							cls:'x-btn-text-icon',
				            icon:'<%=contextPath%>/images/add.gif',
							handler:addRole
						}, '-', {
							id :'saveDicButton',
							text :'保存',
				            cls:'x-btn-text-icon',
				            icon:'<%=contextPath%>/images/save.gif',
							handler:formSubmit
						}, '-', {
							id :'deleteDicButton',
							text :'删除',
				            cls:'x-btn-text-icon',
				            icon:'<%=contextPath%>/images/delete.gif',
							handler:deleteRole
						} ]
		 });
		   			
		// 基本信息form 
       	formBase = new Ext.FormPanel({
				frame :true,
				labelAlign :'right',
				waitMsgTarget :true,
				autoScroll :true,
				method :'POST',
				url :'zxkhbz!save.do',
				items : [{
					xtype :'textfield',
					fieldLabel :'*班组名称',
					name :'username',
					allowBlank :false,
					xtype:'textfield',
					width :200,
					readOnly:true,
					emptyText:'点击左侧班组列表获得'
				},{
					xtype :'textfield',
					fieldLabel :'*考核日期',
					name :'entity.strKhrq',
					allowBlank :false,
					xtype:'datefield',
					format:'Y-m',
					width :200,
					style:'ime-mode:disabled;',
					emptyText:'格式：YYYY-MM(2010-09)'
				},{
					xtype :'textfield',
					fieldLabel :'*分数',
					name :'entity.fs',
					xtype:'numberfield',
					allowBlank :false,
					decimalPrecision:2,   
					maxLength :8,
					width :200,
					emptyText:'请输入分数'					
				}, {
            		xtype:'textarea',
					fieldLabel:'原因',
           			name:'entity.fsyy',
            		width:459,
            		height:100,
            		enableFont:false,
            		maxLength:200
				}, {
					xtype :'hidden',//hidden
					name :'entity.id',
					value :'0'
				}, {
					xtype :'hidden',
					name :'entity.bzid'
				}]
			});
			
			tab3firstBuild();
			
			// tab页设置
            contentTabs = new Ext.Panel({   
                activeTab: 0,   
                height:600,   
                border: true,   
                deferredRender: false,   
                tabPosition: 'top',
                items: [{
                	id:'role1',
                    autoScroll: true,   
                    icon:'<%=contextPath%>/images/delete.gif',
                    items:[toolbar1,formBase,gridUser]   
                }]   
            });   

         	// layout
            layout = new Ext.Viewport({   
                layout: 'border',   
                margins:'5 0 5 5',   
                items: [   
                 {  region: 'west',
                    collapsible: true,   
                    split: true,   
                    width: 200,    
                    minSize: 175,   
                    maxSize: 400,   
                    title: '班组列表',   
                    layout: {   
                        type: 'accordion'
                    },
                    items:[treeRole]
                },{ region: 'center',   
                    title: '班组考评',
                    height:800,
                    layout:'fit',
                    items:[contentTabs]  
                }]   
            }); 
            tempRoleId3 = "0";

       };
       
       // 初始化的时候先放个空
       var tab3firstBuild = function() {
       
       		// 加载分页数据
			storeGrid = new Ext.data.Store({
				proxy :new Ext.data.HttpProxy( {
					url :"zxkhbz!getListExt.do"
				}),
				reader :new Ext.data.JsonReader( {
					root :'result',
					totalProperty :'totalCount',
					// id : 'id',
					fields : [ 'id', 'bzid', 'khrq','fsyy','fs']
				}),
			    listeners:{"beforeload":function(store,records){
			    	store.baseParams.bzid = formBase.getForm().findField("entity.bzid").getValue();
			    }},
				//autoLoad:true,
				remoteSort :false
			});
	
			// 创建GridPanel
			gridUser = new Ext.grid.GridPanel({
				titleVisible:false,
				store :storeGrid,
				height:320,
				frame:true,
        		columns:[
            		new Ext.grid.RowNumberer(),
    			{
				header :"序号",
					width :50,
					dataIndex :'id',
					sortable :true,
					hidden :true
				}, {
					header :"考核日期",
					width :30,
					dataIndex :'khrq',
					sortable :true,
					align:'center'
				}, {
					header :"分数",
					width :20,
					dataIndex :'fs',
					sortable :true					
				}, {
					header :"原因",
					dataIndex :'fsyy',
					sortable :true
				}],
				//sm : new Ext.grid.CheckboxSelectionModel (),
				loadMask : {msg :'载入中,请稍候...'},
				viewConfig:{forceFit:true},
								bbar :new Ext.PagingToolbar( {
					id:'b',
					pageSize :10,
					store :storeGrid,
					displayInfo :true,
					displayMsg :'显示 {0} - {1} 共 {2} 条',
					emptyMsg :"没有数据显示！",
					beforePageText :"页码 ",
					afterPageText :"共 {0} 页",
					firstText :"首页",
					lastText :"末页",
					nextText :"下一页",
					prevText :"上一页",
					refreshText :"刷新"
				}),
				listeners:{"dblclick":function(e){ 
					
					if (gridUser.getSelectionModel().hasSelection()) {
					
						var records = gridUser.getSelectionModel().getSelections();
		        		formBase.getForm().findField("entity.id").setValue(records[0].data["id"]);
		        		formBase.getForm().findField("entity.fsyy").setValue(records[0].data["fsyy"]);
		        		formBase.getForm().findField("entity.fs").setValue(records[0].data["fs"]);
						formBase.getForm().findField("entity.strKhrq").setValue(records[0].data["khrq"]);
					}
                }}
			});
       }

	// 新建时先清空	   			
	function addRole() {
		formBase.getForm().findField("entity.id").setValue("0");
		formBase.getForm().findField("entity.fsyy").setValue("");
		formBase.getForm().findField("entity.fs").setValue("");
		formBase.getForm().findField("entity.strKhrq").setValue(ym);
	}
	
	// 保存
	function formSubmit() {
	
		if (!formBase.form.isValid()) {
			return false;
		}

		formBase.getForm().submit({
			waitMsg :'保存中...',
			success : function(form, action) {
				Ext.MessageBox.alert('提示', action.result.msg);
				storeGrid.reload();
				addRole();
			},
			failure : function() {
				addRole();
				storeGrid.reload();
				Ext.MessageBox.alert('提示', '保存成功。');
			}
	
		});
	
	};
	
	// 删除
	function deleteRole() {

		if (gridUser.getSelectionModel().hasSelection()) {
		
			var records = gridUser.getSelectionModel().getSelections();
			
			if (records.length == 0) {
				Ext.MessageBox.alert('提示', '请选择要删除的记录。');
				
			} else {
				Ext.MessageBox.confirm('提示', "确认要删除选中记录？", toDeleteRole);
			}
		
		} else {
			Ext.MessageBox.alert('提示', '请选择要删除的记录。');
		}
		
	}

	// 确定要删除后执行事件
	function toDeleteRole(btn) {

		if (btn == 'yes') {
		
			var records = gridUser.getSelectionModel().getSelections();

			Ext.Ajax.request({
				method :'POST',
				url :'zxkhbz!delete.do',
				params : {id:records[0].data["id"]},
				success : function(response, options) {
					var obj = Ext.decode(response.responseText);
					Ext.MessageBox.alert('提示', obj["msg"]);
					addRole();
					storeGrid.reload();

				},
				failure : function(response, options) {
					Ext.MessageBox.alert('提示 ', '删除失败。');
				}
			});
		}
	
	}
    </script>  
</head>  
  
<body>  
      <div id="Container">  
          <div id="ToolBar"></div>  
          <div id="Content"></div>  
          <div id="StatuBar"></div>  
      </div>  
</body>  
</html>


